package com.zlpc.service;

import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zlpc.pojo.Information;

@Service
public class SearchService {

	@SuppressWarnings("deprecation")
	@Autowired
	private HttpSolrServer server;

    // 1 查询单条信息
	public Information query(String keyWord) throws Exception {
		SolrQuery query = new SolrQuery();
		query.setQuery("job_title:" + keyWord);
		query.setStart(0);
		query.setRows(1);
		QueryResponse response = server.query(query);
		List<Information> list = response.getBeans(Information.class);
		return list.get(0);
	}
	
	// 2 查询单条信息
	public Long list(String keyWord) throws Exception  {
        // 2.1 创建solr的查询对象
        SolrQuery sq=new SolrQuery();
        // 2.2 设置查询条件
        sq.set("q","job_title:"+keyWord);
        // 2.3 查询
        QueryResponse qr=server.query(sq);
        // 2.4 获取查询结果
        SolrDocumentList sds=qr.getResults();
        // 2.5获取查询的记录数
        long total=sds.getNumFound();
        return total;

	}
	

}
